<template>
    <div class="familyhistory">
      <button  type="button" class="el-button el-button--text el-button--small" style="float: left; padding: 3px 0px;" @click="handleadd" v-if="!disabled"><!----><!----><span>添加家族病史</span></button>
      <el-dialog
        title="添加家族史"
        :visible.sync="dialogFormVisible"
        width="500px"

      >
        <el-form ref="form" :model="form" label-width="80px" >
          <el-form-item label="病种" >
            <el-select v-model="diseases"   placeholder="病种"  >
              <el-option
                v-for="item in diseasess"
                :key="item.dcNo"
                :label="item.name"
                :value="item.dcNo">
              </el-option>
            </el-select>

          </el-form-item>
          <el-form-item label="家族关系" >
            <el-select v-model="familyRelationship"   placeholder="家族关系"  >
              <el-option
                v-for="item in familyRelationships"
                :key="item.dcNo"
                :label="item.name"
                :value="item.dcNo">
              </el-option>
            </el-select>

          </el-form-item>
          <el-form-item label="确诊时间" >
            <el-date-picker
              v-model="time"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="close">取 消</el-button>
          <el-button type="primary"  @click="Saveadd">确 定</el-button>
        </div>


      </el-dialog>
      <el-table
        :data="list"
        width="100%"

      >

        <el-table-column
          show-overflow-tooltip
          label="老人编号"
          prop="elderlyCode"
        ></el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="diseasesName"
          label="疾病类型"

        ></el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="familyRelationshipName"
          label="家族关系"

        ></el-table-column>



        <el-table-column  label="确诊时间">
          <template slot-scope="scope">
            {{scope.row.diagnosisTime | parseTime('{y}-{m}-{d}')}}
          </template>
        </el-table-column>

        <el-table-column label="操作" prop="disabled"   v-if="!disabled"  show-overflow-tooltip width="200px">
          <template slot-scope="scope">
            <el-button type="text"  @click="doDelete(scope.$index, scope.row)" >删除<i class="el-icon-delete"></i></el-button>

          </template>


        </el-table-column>
      </el-table>
    </div>
</template>

<script>
    import { add } from "@/api/familyhistory"
    import { search } from "@/api/familyhistory"
    import { del } from "@/api/familyhistory"
    import { parseTime} from "@/utils/index";
    export default {
        name: "familyhistory",
        filters:{
            parseTime:parseTime
        },
        props: ["elderlyCode","dictionary","disabled"],
        data(){
            return{
                dialogFormVisible:false,
                list:[],
                time:"",
                listLoading:true,
                diseases:"",
                diseasess:{

                },
                familyRelationship:"",
                familyRelationships:"",
                form:{
                    diagnosisTime:"",
                    diseases:"",
                    elderlyCode:"",
                    familyRelationship:"",
                }
            }

        },
        created(){

            for(var i=0;i<this.dictionary.length;i++){

                if(this.dictionary[i].mark=="diseases"){
                    this.diseasess=(this.dictionary[i].childs)

                }else  if(this.dictionary[i].mark=="familyRelation"){
                    this.familyRelationships=(this.dictionary[i].childs)
                }

            }
                this.search()
        },
        methods:{
            handleadd(){
                this.time=""
                this.diseases=""
                this.familyRelationship=""
                this.dialogFormVisible=true
            },
            Saveadd(){

                this.form.elderlyCode=this.elderlyCode
                this.form.diagnosisTime=this.time
                this.form.diseases=this.diseases
                this.form.familyRelationship=this.familyRelationship
                console.log(this.form)
                if(this.elderlyCode){
                    this.$refs["form"].validate(async (valid) => {
                        if (valid) {
                            const {code} = await add(this.form);
                            console.log(code)
                            if(code==200){
                                this.$message({
                                    message: "添加成功",
                                    type: 'success'
                                })
                                this.search()
                            }else {

                            }



                        } else {
                            return false;
                        }
                    });
                }else {
                    this.$message({
                        message: "请先添加基本信息",
                        type: 'warning'
                    })
                }
            },
            search(){

                console.log(this.elderlyCode)
                if(this.elderlyCode){
                    let elderlyCode=this.elderlyCode
                    search({elderlyCode}).then(res =>{
                        console.log(res)
                        this.list=res.data

                    })
                }else {

                }


            },
            doDelete(index,row) {
                let elderlyCode=row.elderlyCode
                let diseases=row.diseases
                let familyRelationship=row.familyRelationship
                console.log(row)
                if (row.id) {
                    this.$baseConfirm("你确定要删除当前项吗", null, async () => {
                        const {code, msg } = await del(({elderlyCode,diseases,familyRelationship,}));
                        if(code==200){
                            this.$baseMessage("删除成功");
                            this.search()
                        }else {
                            this.$baseMessage("删除失败");
                        }
                    });
                }
            },
            close(){
                this.dialogFormVisible=false
            },
        },


    }
</script>

<style scoped>

</style>
